import {defHttp} from '/@/utils/http/axios';
import { useMessage } from "/@/hooks/web/useMessage";
import { useGlobSetting } from '/@/hooks/setting';

const { createConfirm } = useMessage();
const globSetting = useGlobSetting();
const baseUploadUrl = globSetting.uploadUrl;

enum Api {
  list = '/h5/sys/exam_questions/list',
  lists = '/h5/sys/exam_questions/list',
  save='/h5/sys/exam_questions/add',
  edit='/h5/sys/exam_questions/edit',
  deleteOne = '/h5/sys/exam_questions/delete',
  deleteBatch = '/h5/sys/exam_questions/deleteBatch',
  importExcel = '/h5/sys/exam_questions/importExcel',
  exportXls = '/h5/sys/exam_questions/exportXls',
  queryDataById = '/h5/sys/exam_questions/queryById',
  examOptionsList = '/h5/sys/exam_questions/queryExamOptionsByMainId',
  // 公共题目相关接口
  publicList = '/h5/sys/exam_questions_public/list',
  publicEdit = '/h5/sys/exam_questions_public/edit',
  publicSync = '/h5/sys/exam_questions_public/synchronizationData',
  // 文件上传接口
  upload = '/sys/common/upload',
}
/**
 * 导出api
 * @param params
 */
export const getExportUrl = Api.exportXls;

/**
 * 导入api
 */
export const getImportUrl = Api.importExcel;

/**
 * 查询子表数据
 * @param params
 */
export const queryExamOptionsListByMainId = (id) => defHttp.get({url: Api.examOptionsList, params:{ id }});

/**
 * 列表接口
 * @param params
 */
export const list = (params) =>{
  
  return defHttp.get({url: Api.list, params})
}
export const lists = async(param) =>{
  let params = {
    "column": "createTime",
    "order": "desc",
    "pageNo": 1,
    "pageSize": 1000,
    "bankId": param.bankid
}
console.log(params,"paramsparams")
  // options: [
  //   { title: '是', value: '1' },
  //   { title: '否', value: '0' },
  // ]
  let res = await defHttp.get({url: Api.list, params})
  console.log(res,"resresres")
  let data = []
  res.records.forEach(item=>{
    data.push({
      title:item.content,
      value:item.id
    })
  })
  console.log('zqt',res)
  return data
}
  


/**
 * 删除单个
 */
export const deleteOne = (params,handleSuccess) => {
  return defHttp.delete({url: Api.deleteOne, params}, {joinParamsToUrl: true}).then(() => {
    handleSuccess();
  });
}
/**
 * 批量删除
 * @param params
 */
export const batchDelete = (params, handleSuccess) => {
  createConfirm({
    iconType: 'warning',
    title: '确认删除',
    content: '是否删除选中数据',
    okText: '确认',
    cancelText: '取消',
    onOk: () => {
      return defHttp.delete({url: Api.deleteBatch, data: params}, {joinParamsToUrl: true}).then(() => {
        handleSuccess();
      });
    }
  });
}
/**
 * 保存或者更新
 * @param params
 */
export const saveOrUpdate = (params, isUpdate) => {
  let url = isUpdate ? Api.edit : Api.save;
  return defHttp.post({url: url, params});
}

/**
* 根据id查询数据
* @param params
*/
export const queryDataById = (id) => defHttp.get({url: Api.queryDataById, params:{ id }});

/**
 * 公共题目列表接口
 * @param params
 */
export const publicList = (params) => {
  return defHttp.get({url: Api.publicList, params})
}

/**
 * 公共题目编辑接口
 * @param params
 */
export const publicEdit = (params) => {
  return defHttp.post({url: Api.publicEdit, params});
}

/**
 * 公共题目同步接口
 * @param params
 */
export const publicSync = (params) => {
  return defHttp.post({
    url: Api.publicSync, 
    params,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    }
  });
}

/**
 * 文件上传接口
 * @param params
 */
export const uploadFile = (params) => {
  return defHttp.uploadFile(
    { url: `${baseUploadUrl}${Api.upload}` }, 
    params,
    { isReturnResponse: true }  // 必须设置为 true 才能返回响应数据
  );
}

/**
 * 更新题目图片地址
 * @param id 题目ID
 * @param examUrl 图片URL
 */
export const updateExamUrl = (id: string, examUrl: string) => {
  return defHttp.post({
    url: Api.edit,
    params: { id, examUrl }
  });
}

